<?php

/**
 * uniBoard API for file upload
 *
 * Light and fast forum solution for web 2.0-like communities.
 *
 * @package		uniBoard
 * @author		Rafał Pitoń
 * @license		http://www.gnu.org/licenses/gpl-3.0.html
 * @since		Version 0.1
 * 
 * ------------------------------------------------------------------------
 * 
 * $Date: 2010-09-06 01:45:14 +0000 (Mon, 06 Sep 2010) $
 * $Revision: 59 $
 * $Author: rafio.xudb@gmail.com $
 */

// ------------------------------------------------------------------------

class api_file_upload extends class_api{
	
	/**
	 * Main class pointer
	 *
	 * @var object
	 */
	
	private $appCore				= NULL;
	
	/**
	 * File form key
	 *
	 * @var string
	 */
	
	private $file_key				= NULL;
	
	/**
	 * Max upload size in bytes
	 *
	 * @var integer
	 */
	
	private $max_upload_size		= 0;
	
	/**
	 * Array with allowed file extensions
	 *
	 * @var array
	 */
	
	private $allowed_extensions		= array();
	
	/**
	 * Uploaded file extension
	 *
	 * @var integer
	 */
	
	private $file_extension			= 0;
	
	/**
	 * Uploaded file extension as string
	 *
	 * @var string
	 */
	
	private $file_extension_str		= '';
	
	// ------------------------------------------------------------------------
	
	/**
	 * Initialise Api
	 *
	 * @param object $appCore
	 * @param integer $file_key
	 */
	
	function __construct( $appCore, $file_key){
					
		// Store appCore pointer
		$this -> appCore = $appCore;
	
		// Store input name
		$this -> file_key = $file_key;
				
	}
	
	// ------------------------------------------------------------------------
	
	/**
	 * Set Max upload size in KiloBytes
	 *
	 * @param integer $new_val
	 */
	
	function setUploadSize( $new_val){
		
		// Set variable
		$this -> max_upload_size = 1024 * $new_val;
		
	}
	
	// ------------------------------------------------------------------------
	
	/**
	 * Allowed extensions
	 *
	 * @param array $allowed_extensions
	 */
	
	function setExtensions( $allowed_extensions){
		
		// Set variable
		$this -> allowed_extensions = $allowed_extensions;
		
	}
	
	// ------------------------------------------------------------------------
	
	/**
	 * Attempts to catch uploaded file
	 *
	 */
	
	function catchFile(){
		
		// File uploaded?
		if ( isset( $_FILES[$this -> file_key]) && is_uploaded_file( $_FILES[$this -> file_key]['tmp_name']))
		{
			// File size?
			if ( $_FILES[$this -> file_key]['size'] < $this -> max_upload_size)
			{
				// Found file extension?
				$correct_extension = false;
				
				// Reiterate our extensions
				foreach ( $this -> allowed_extensions as $extension_id => $extension_data)
				{
					// Store extension id
					$this -> file_extension = $extension_id;
					
					// Extension string
					$this -> file_extension_str = strtolower( substr( $_FILES[$this -> file_key]['name'], strlen( $_FILES[$this -> file_key]['name']) - strlen( $extension_data)));
							
					// Check with mime too?
					if ( is_array( $extension_data))
					{
						// Compare extension and mime
						if( $extension_data[0] == strtolower( substr( $_FILES[$this -> file_key]['name'], strlen( $_FILES[$this -> file_key]['name']) - strlen( $extension_data))) && $extension_data[1] == $_FILES[$this -> file_key]['type'])
						{
							// Extension found
							$correct_extension = true;
						
							// Break function
							return;
						}
					}
					else
					{
						// Compare extension
						if( $extension_data == strtolower( substr( $_FILES[$this -> file_key]['name'], strlen( $_FILES[$this -> file_key]['name']) - strlen( $extension_data))))
						{
							// Extension found
							$correct_extension = true;
							
							// Break function
							return;
						}
					}
				}
				
				// Found extension
				if ( !$correct_extension)
				{
					$this -> setError( $this -> appCore -> lang['file_upload_not_allowed']);
				}
			}
			else
			{
				// File too big
				$this -> setError( $this -> appCore -> lang['file_upload_too_big']);					
			}
		}
		else
		{
			// File not uploaded
			$this -> setError( $this -> appCore -> lang['file_upload_no_file']);	
		}
		
	}
	
	// ------------------------------------------------------------------------
	
	/**
	 * Returns uploaded file name
	 *
	 * @param bool $full
	 * @return string
	 */
	
	function getFileName( $full = true){
		
		// Return $_FILES variable
		return ( $full ? $_FILES[$this -> file_key]['name'] : substr( $_FILES[$this -> file_key]['name'], 0, strlen( $_FILES[$this -> file_key]['name']) - strlen( $this -> file_extension_str)));
		
	}
	
	// ------------------------------------------------------------------------
	
	/**
	 * Returns uploaded file localisation in TMP
	 *
	 * @return string
	 */
	
	function getFilePath(){
		
		// Return $_FILES variable
		return $_FILES[$this -> file_key]['tmp_name'];
		
	}
	
	// ------------------------------------------------------------------------
	
	/**
	 * Returns uploaded file size
	 *
	 * @return integer
	 */
	
	function getFileSize(){
		
		// Return $_FILES variable
		return $_FILES[$this -> file_key]['size'];
		
	}
	
	// ------------------------------------------------------------------------
	
	/**
	 * Returns file extension id
	 *
	 * @return integer
	 */
	
	function getFileExtensionId(){
		
		// Return our variable
		return $this -> file_extension;
		
	}
	
	// ------------------------------------------------------------------------
	
	/**
	 * Returns file extension
	 *
	 * @return string
	 */
	
	function getFileExtension(){
		
		// Return our variable
		return $this -> file_extension_str;
		
	}
	
	// ------------------------------------------------------------------------
	
	/**
	 * Move uploaded file to new localistion
	 *
	 * @param string $directory
	 * @param string $file_name
	 */
	
	function moveFile( $directory, $file_name){

		// Move uploaded file?
		if ( !$this -> error)
		{
			move_uploaded_file( $_FILES[$this -> file_key]['tmp_name'], $directory . $file_name);
		}
		
	}
	
}